/**
 * 
 */
package au.edu.swin.videoshop.ejb.bean;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import au.edu.swin.videoshop.bean.Customer;
import au.edu.swin.videoshop.bean.Reservation;
import au.edu.swin.videoshop.bean.ReservationStatus;
import au.edu.swin.videoshop.persistence.ReservationEntity;
import au.edu.swin.videoshop.service.ReservationService;

/**
 * @author julie
 *
 */
@Stateless
public class ReservationServiceBean implements ReservationService {

	@PersistenceContext
	private EntityManager em;

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.ReservationService#addReservation(au.edu.swin.videoshop.bean.Reservation)
	 */
	public void addReservation(Reservation reservation) {
		reservation.setStatus(ReservationStatus.OPEN);
		TransferService service = new TransferService();
		ReservationEntity reservationEntity = service.toReservationEntity(reservation);
		em.persist(reservationEntity);
	}
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.ReservationService#cancelReservation(au.edu.swin.videoshop.bean.Customer, long)
	 */
	public void cancelReservation(Customer customer, long reservationId) {
		Query query = em.createNamedQuery("cancelReservation");
		query.setParameter("customerId", customer.getCustomerId());
		query.setParameter("reservationId", reservationId);
		query.executeUpdate();
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.ReservationService#findReservationsByCustomerId(long)
	 */
	public List<Reservation> findReservationsByCustomerId(long customerId) {
		Query query = em.createNamedQuery("findReservationsByCustomerId");
		query.setParameter("customerId", customerId);
		List entityList = query.getResultList();
		TransferService service = new TransferService();
		return service.toReservationTransferObjectList(entityList);
	}
}
